package ru.rchervot.sanasar.client;

import ru.rchervot.sanasar.client.event.EventSettingsChange;
import ru.rchervot.sanasar.client.gin.GinjectorView;
import ru.rchervot.sanasar.client.model.DefaultUserSettings;
import ru.rchervot.sanasar.client.model.UserSettings;
import ru.rchervot.sanasar.client.place.ManagerPlaces;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.History;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Sanasar implements EntryPoint {

	private final GinjectorView injector = GWT.create(GinjectorView.class);
	
	public void onModuleLoad() {
		injector.getService(); // in order to initialize, so service could catch fired event below
		
		UserSettings settings = loadUserSettings(); 
		
		injector.getEventBus().fireEvent(EventSettingsChange.build(settings));
		
		injector.getFactoryQuery().setPageSize(settings.getPageSize());
		injector.getFactoryModuleCandidates().setSettings(settings);
		injector.getFactoryModuleSettings().setSettings(settings);
		
		ManagerPlaces managerPlaces = injector.getManagerPlaces();
		History.addValueChangeHandler(managerPlaces);
		
		//start application here
		History.fireCurrentHistoryState();
	}
	
	private UserSettings loadUserSettings() {
		UserSettings settings = new DefaultUserSettings(); // TODO we should use service to load settings
		
		settings.setPageSize(50);
		
		return settings;
	}
	
}

